package com.risenb.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.risenb.bean.Manager;
import com.risenb.bean.ManagerRole;
import com.risenb.bean.PermissionRole;
import com.risenb.bean.ResultMsg;
import com.risenb.bean.Role;
import com.risenb.constant.Const;
import com.risenb.service.GroupService;
import com.risenb.service.ManagerRoleService;
import com.risenb.service.ModuleService;
import com.risenb.service.PermissionRoleService;
import com.risenb.service.PermissionService;
import com.risenb.service.RoleService;

@Controller
@RequestMapping("role")
public class RoleManageController extends CustomerBaseControllor {
	
	@Autowired
	RoleService roleService;
	
	@Autowired
	PermissionService permissionService;
	
	@Autowired
	GroupService groupService;
	
	@Autowired
	ModuleService moduleService;
	
	@Autowired
	PermissionRoleService permissionRoleService;
	
	@Autowired
	ManagerRoleService managerRoleService;

	// 获取所有角色
	@RequestMapping("fr")
	public ModelAndView findRoles(@RequestBody @ModelAttribute Role role, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		ModelAndView mv = new ModelAndView("manage/rolemanage");
		List<Role> roles = roleService.queryAll(role);
		mv.addObject("roles", roles);
		return mv;
	}

	// 获取单个角色
	@RequestMapping(value = "gr")
	public ModelAndView findRoleById(Integer rid, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		ModelAndView mv = new ModelAndView("manage/editrole");
		if (rid != null) {
			Role role = roleService.queryRoleById(rid);
			mv.addObject("role", role);
			List<PermissionRole> pr = role.getPr();
			String psid = "";
			String psname = "";
			for (int i = 0; i < pr.size(); i++) {
				if (pr.get(i).getPermission() != null) {
					psid += pr.get(i).getPermission().getId() + ",";
					psname += pr.get(i).getPermission().getTitle() + ",";
				}
			}
			if (psid.length() > 0) {
				psid = psid.substring(0, psid.length() - 1);
				psname = psname.substring(0, psname.length() - 1);
			}
			mv.addObject("psid", psid);
			mv.addObject("psname", psname);
		}
		mv.addObject("ps", permissionService.queryAll());
		return mv;
	}

	// 编辑角色
	@RequestMapping(value = "er")
	public void editRole(@RequestBody @ModelAttribute Role role, String pids, HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws IOException {
		ResultMsg resultMsg = null;
		Manager manager = (Manager) session.getAttribute(Const.SESSION_MANAGER);
		int row = 0;
		role.setOperator(((Manager) session.getAttribute(Const.SESSION_MANAGER)).getId());
		role.setOperatime(new Date());
		if (role.getId() != null) {
			row = roleService.modifyRoleById(role);
		} else {
			row = roleService.createRole(role);
		}
		if (row > 0) {
			permissionRoleService.removePermissionRoleByRoleId(role.getId());
			String[] ps = pids.split(",");
			for (int i = 0; i < ps.length; i++) {
				if (!StringUtils.isBlank(ps[i])) {
					PermissionRole pr = new PermissionRole();
					pr.setOperator(manager.getId());
					pr.setOperatime(new Date());
					pr.setPermissionid(Integer.parseInt(ps[i]));
					pr.setRoleid(role.getId());
					permissionRoleService.createPermissionRole(pr);
				}
			}
			resultMsg = new ResultMsg("200", "操作完成", "", "role", "", "", "操作完成", true);
		} else {
			resultMsg = new ResultMsg("300", "操作失败", "", "role", "", "", "操作失败", true);
		}
		returnJson(resultMsg);
	}

	// 冻结角色
	@RequestMapping(value = "dr")
	public void dongjieRole(Integer rid, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
		try {
			int row = 0;
			Role role = roleService.queryRoleById(rid);
			role.setOperator(((Manager) session.getAttribute(Const.SESSION_MANAGER)).getId());
			role.setOperatime(new Date());
			if (role.getStatus() == null || role.getStatus() == 1) {
				role.setStatus(0);
			} else {
				role.setStatus(1);
			}
			row = roleService.modifyRoleById(role);
			ResultMsg resultMsg = null;
			if (row > 0) {
				resultMsg = new ResultMsg("200", "操作完成", "", "role", "", "", "");
			} else {
				resultMsg = new ResultMsg("300", "操作失败", "", "role", "", "", "操作失败");
			}
			returnJson(resultMsg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//
	@RequestMapping(value = "delr")
	public void deleteRole(String ids, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
		List<String> list = Arrays.asList(ids.split(","));
		int row = roleService.removeRoleByIds(list);
		ResultMsg resultMsg = null;
		if (row > 0) {
			resultMsg = new ResultMsg("200", "操作完成", "", "role", "", "", "");
		} else {
			resultMsg = new ResultMsg("300", "操作失败", "", "role", "", "", "操作失败");
		}
		returnJson(resultMsg);
	}

	@RequestMapping(value = "fmr")
	public void getManagerRole(Integer mid, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		List<ManagerRole> mrs = managerRoleService.queryRoleByManager(mid);
		returnJson(new Gson().toJson(mrs));
	}
}
